#ifndef _POWER_CHARGER_H_
#define _POWER_CHARGER_H_

#include "lv_drv_conf.h"

#if USE_POWER_MODULE
#include <stdint.h>

/* ============================================================================= */
/*  TYPES */
/* ============================================================================= */
typedef unsigned char BOOL;

typedef enum
{
    /// Starting state before the charger detection has occured once
    PMIC_CHARGER_UNKNOWN,            // 0
    /// No Charger is plugged
    PMIC_CHARGER_UNPLUGGED,          // 1
    /// Charger is plugged, but charge is not running
    PMIC_CHARGER_PLUGGED,            // 2
    /// Charger is plugged, charge in precharge phase (whe souldn't be on anyway)
    PMIC_CHARGER_PRECHARGE,          // 3
    /// Charger is plugged, charge in fast mode (constant current)
    PMIC_CHARGER_FAST_CHARGE,        // 4
    /// Charger is plugged, charge in pulsed mode (constant voltage, active)
    PMIC_CHARGER_PULSED_CHARGE,      // 5
    /// Charger is plugged, charge in pulsed mode (constant voltage, inactive)
    PMIC_CHARGER_PULSED_CHARGE_WAIT, // 6
    /// Charger is plugged, charge done.
    PMIC_CHARGER_FULL_CHARGE,        // 7
    /// Charger is plugged, charge stopped for safety (error).
    PMIC_CHARGER_ERROR,              // 8
    /// Charger is plugged, charge stopped for safety (timeout).
    PMIC_CHARGER_TIMED_OUT,          // 9
    /// Charger is plugged, charge stopped for safety (temperature).
    PMIC_CHARGER_TEMPERATURE_ERROR,  // 10
    /// Charger is plugged, charge stopped for safety (voltage).
    PMIC_CHARGER_VOLTAGE_ERROR,      // 11

    PMIC_CHARGER_STATUS_QTY
} PMIC_CHARGER_STATUS_T;

typedef enum
{
    /// Charge with a  100 mA current
    PMIC_CHARGER_100MA,
    /// Charge with a 200 mA current
    PMIC_CHARGER_200MA,
    /// Charge with a 300 mA current
    PMIC_CHARGER_300MA,
    /// Charge with a 400 mA current
    PMIC_CHARGER_400MA,
    /// Charge with a 450 mA current
    PMIC_CHARGER_450MA,
    /// Charge with a 500 mA current
    PMIC_CHARGER_500MA,
    /// Charge with a 550 mA current
    PMIC_CHARGER_550MA,
    /// Charge with a 600 mA current
    PMIC_CHARGER_600MA,
    /// Charge with a 650 mA current
    PMIC_CHARGER_650MA,
    /// Charge with a 700 mA current
    PMIC_CHARGER_700MA,
    /// Charge with a 750 mA current
    PMIC_CHARGER_750MA,
    /// Charge with a 800 mA current
    PMIC_CHARGER_800MA,
    /// Charge with a 850 mA current
    PMIC_CHARGER_850MA,
    /// Charge with a 900 mA current
    PMIC_CHARGER_900MA,
    /// Charge with a 950 mA current
    PMIC_CHARGER_950MA,
    /// Charge with a 1000 mA current
    PMIC_CHARGER_1000MA,
} PMIC_CHARGE_CURRENT_T;

typedef enum {
  CHR_STATUS_UNKNOWN,
  CHR_STATUS_AC_OFF,
  CHR_STATUS_AC_ON,
} PMU_CHR_STATUS_T;

typedef enum {
  /*/ Charger is disconnected */
  PMIC_CHARGE_DC_OFF,
  /*/ Charger is connected */
  PMIC_CHARGE_DC_ON,
  /*/ Charger called by funtion timer */
  PMIC_CHARGE_TIMER,
  /*/ Charger called by pmic_SetChargeCurrent (manual on/off of charge) */
  PMIC_CHARGE_CURRENT,

  PMIC_CHARGE_EVENT_QTY
} PMIC_CHARGE_EVENT_T;

typedef struct {
  PMIC_CHARGER_STATUS_T status;
  PMIC_CHARGE_CURRENT_T current;
  uint16_t batteryLevel;
#if 0
  UINT16 pulsedOnCount;
  UINT16 pulsedOffCount;

  UINT16 pulsedCycleCount;
  UINT16 pulsedOnMeanCount;
  UINT16 pulsedOffMeanCount;

  UINT32 startTime;
  UINT32 lastWTime;

  BOOL highActivityState;
#endif
} PMIC_CHARGER_STATE_T;

void pmic_InitCharger(void);
void pmic_RestoreChargerAtPowerOff(void);
void pmic_ChargerPlugIn(void);

typedef enum {
  PMIC_PROFILE_MODE_NORMAL = 0,
  PMIC_PROFILE_MODE_LOWPOWER,
  PMIC_PROFILE_MODE_QTY,
} PMIC_PROFILE_MODE_T;

int pm812_get_charger_status(void);
unsigned int pm812_get_batt_vol(void);
int pm812_get_battert_status(void);
void ChargerManager(void);
void charger_dump_config(void);
uint16_t pmic_GetBatteryLevel(uint8_t *pPercent);
uint8_t pmic_BatteryLevelToPercent(uint16_t batteryLevel);
PMIC_CHARGER_STATUS_T pmic_GetChargerStatus(void);
uint8_t pmic_GetPowerDownVolt(void);
uint16_t pmic_GetGpadcBatteryLevel(void);

#endif

#endif
